/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */

package ctrLayer;

import java.util.ArrayList;

import modelLayer.Address;
import dbLayer.DbAddress;
import dbLayer.DbMaxId;

public class CtrAddress {
	
	private DbAddress dbAddress;
	
	public CtrAddress(){
		dbAddress = new DbAddress();
	}
	/**
	 * Retrieves a address by address id.
	 * @param int addressId
	 * @return Address object
	 */
	public Address retrieveAddress(int addressId) {
		return dbAddress.retrieveById(addressId);
	}
	
	/**
	 * Retrieves a address by the location name.
	 * @param String locationName
	 * @return Address object
	 */
	public Address retrieveAddressByLocation(String locationName) {
		return dbAddress.retrieveByLocation(locationName);
	}
	
	/**
	 * Creates a address object and inserts
	 * it into the database.
	 * Then returns the address object.
	 * @param String locationName
	 * @param String street
	 * @param int houseNo
	 * @param String postalCode
	 * @param String city
	 * @param String country
	 * @param int sequence
	 * @return Address object
	 * @throws Exception
	 */
	public Address create(String locationName, String street, String houseNo, String postalCode, String city, String country, int sequence) throws Exception {
		Address aObj = new Address(locationName, street, houseNo, postalCode, city, country, sequence);
		int id = DbMaxId.getMaxID("addressId", "address") + 1;
		aObj.setAddressId(id);
		dbAddress.insert(aObj);
		return dbAddress.retrieveById(id);
	}
	
	public void insertAddress(Address aObj) throws Exception{
		int id = DbMaxId.getMaxID("addressId", "address") + 1;
		aObj.setAddressId(id);
		dbAddress.insert(aObj);
	}

	
	public Address createWOInsert(String locationName, String street, String houseNo, String postalCode, String city, String country, int sequence) {
		Address aObj = new Address(locationName, street, houseNo, postalCode, city, country, sequence);
		return aObj;
	}
	
	/**
	 * Creates a address object for a booking.
	 * @param Address address
	 * @param int bookingId
	 * @throws Exception
	 */
	public void createAddressBooking(Address address, int addressType , int bookingId) throws Exception {
		dbAddress.insertBookingAddress(address, addressType ,bookingId);
	}
	/**
	 * Updates a businesspartners address
	 * @param Address address
	 * @param int partnerId
	 */
	public void updateBpAddress(Address address, int partnerId) {
		dbAddress.updateBpAddress(address, partnerId);
	}
	/**
	 * Updates a address in the database.
	 * @param Address address
	 */
	public void update(Address address) {
		dbAddress.update(address);
	}
	/**
	 * Deletes a address from the database.
	 * @param Address address
	 */
	public void delete(Address address) {
		dbAddress.delete(address);
	}
	
	/**
	 * Method for retrieving a list of addresses based on their postalCode
	 * @param text
	 * @return
	 */
	public ArrayList<String> retrieveCodesByString(String text) {
		return dbAddress.retrievePCListByCode(text);
	}
	
	/**
	 * Method for retrieving a country based on its postalcode
	 * @param postalCode
	 * @return
	 */
	public String retrieveCountryByCode(String postalCode) {
		return dbAddress.retrieveCountryByCode(postalCode);
	}
	/**
	 * Method for retrieving a city based on its postalcode
	 * @param string
	 * @return
	 */
	public String retrieveCityByCode(String string) {
		return dbAddress.retrieveCityByCode(string);
	}
	
}
